package com.example.compose

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun TaskItem(
    modifier: Modifier = Modifier,
    isCompleted: Boolean = false,
    title: String = "No title",
    dueDateText: String = "Due Date: no due date",
    cardHeight: Dp = 72.dp,
    borderRadius: Dp = 8.dp,
    cardBackgroundColor: Color = Color(0xFFF8FAFC),
    indicatorWidth: Dp = 8.dp,
    completedIndicatorColor: Color = Color(0xFF5FD788),
    pendingIndicatorColor: Color = Color(0xFFE2E8F0),
    titleColor: Color = Color(0xFF31394F),
    spacerWidth: Dp = 16.dp,
    titleDueDateSpacing: Dp = 8.dp,
    outerIndicatorDiameter: Dp = 28.dp,
    innerIndicatorDiameter: Dp = 20.dp,
    titleTextStyle: TextStyle = TextStyle(
        fontSize = 16.sp,
        fontWeight = FontWeight.Bold,
        color = titleColor
    ),
    dueDateTextStyle: TextStyle = TextStyle(
        fontWeight = FontWeight.Medium,
        fontSize = 14.sp,
        color = titleColor.copy(alpha = 0.6f)
    ),
    checkedIconDrawable: ImageVector = Icons.Filled.Check,
    testTag: String = "",
    onClicked: () -> Unit = {}
) {
    Card(
        onClick = onClicked, modifier = modifier
            .height(cardHeight)
            .fillMaxWidth(),
        shape = RoundedCornerShape(borderRadius),
        colors = CardDefaults.cardColors(containerColor = cardBackgroundColor)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .width(indicatorWidth)
                    .background(
                        if (isCompleted) {
                            completedIndicatorColor
                        } else {
                            pendingIndicatorColor
                        }
                    )
            )
            Spacer(modifier = Modifier.width(spacerWidth))
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(text = title, style = titleTextStyle)
                Spacer(modifier = Modifier.height(titleDueDateSpacing))
                Text(text = dueDateText, style = dueDateTextStyle)
            }
            Box(
                modifier = Modifier.size(outerIndicatorDiameter),
                contentAlignment = Alignment.Center
            ) {
                Canvas(modifier = Modifier.matchParentSize()) {
                    val center = Offset(x = size.width / 2, y = size.height / 2)
                    val circleColor =
                        if (isCompleted) completedIndicatorColor else pendingIndicatorColor
                    drawCircle(
                        color = circleColor.copy(alpha = 0.3f),
                        center = center,
                        radius = size.minDimension / 2
                    )
                    drawCircle(
                        color = circleColor,
                        center = center,
                        radius = innerIndicatorDiameter.toPx() / 2
                    )
                }
                if (isCompleted) {
                    Icon(
                        imageVector = checkedIconDrawable,
                        contentDescription = null,
                        modifier = Modifier
                            .testTag(testTag)
                            .padding(6.dp),
                        tint = Color.White
                    )
                }
            }
            Spacer(modifier = Modifier.width(spacerWidth))
        }
    }
}

@Preview
@Composable
private fun TodoItemPreview() {
    val clickState = remember {
        mutableStateOf(false)
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
            .background(Color(0xffb0b0b1))
            .padding(24.dp)
    ) {
        Column {
            TaskItem(
                title = "这是一个标题",
                dueDateText = "时间: 20224年6月18号",
                isCompleted = clickState.value,
                onClicked = {
                    clickState.value = !clickState.value
                }
            )
            Spacer(modifier = Modifier.height(4.dp))
            TaskItem(
                title = "这是一个标题",
                dueDateText = "时间: 20224年6月18号",
                isCompleted = clickState.value,
                onClicked = {
                    clickState.value = !clickState.value
                }
            )
        }

    }
}